package com.fingard.dsp.bank.directbank.fxb01;

import com.alibaba.fastjson.JSONObject;
import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.dsp.bank.directbank.fxb01.util.MACUtil;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpReceiver;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;

import java.io.File;
import java.util.Date;
import java.util.HashMap;

public class FXB01Base extends DirectBase implements ITcpClient {

    protected static final String INSERTRECEIPT = "INSERT INTO REQSEQIDMAPPING(URID,BANKCODE,REQSEQID,BANKREQSEQID,TRANSDATE) values (?,?,?,?,?);";
    protected static final String SELECTRECEIPT = "SELECT BANKREQSEQID,TRANSDATE FROM REQSEQIDMAPPING WHERE REQSEQID=? and BANKCODE=?;";

    protected void writeCommonHeader(JSONObject json, FXB01Header fuxinbank01Header) throws Exception {
        HashMap<String,String> header = new HashMap<String,String>();
        header.put("SERVICE_ID",fuxinbank01Header.transCode);
        header.put("SCENE_ID","01");
        header.put("TRAN_MODE","ONLINE");
        header.put("SERVICE_CODE",fuxinbank01Header.transCode);//服务码
        header.put("MESSAGE_TYPE","");//消息类型
        header.put("MESSAGE_CODE","");//消息码
        header.put("SOURCE_TYPE",this.getBankConfig().getValueAsString("Systemcode"));//填写规则：系统编号
        header.put("BRANCH_ID",fuxinbank01Header.branchId);//交易机构号

        header.put("USER_ID",bankFront.operator);//交易柜员号
        header.put("TRAN_DATE", Format.Date8Format.format(new Date()));//交易日期
        header.put("TRAN_TIMESTAMP",Format.Time9Format.format(new Date()));//交易时间
        header.put("USER_LANG","CHINESE");
        header.put("SEQ_NO",this.getBankConfig().getValueAsString("Systemcode") + Format.Date8Format.format(new Date()) + getTimeStr().substring(getTimeStr().length()-8,getTimeStr().length()));//系统编号(6位)+日期(8位)+序号(8位)
        header.put("SYSTEM_ID","");//发起方系统编码
        header.put("COMPANY","");//法人代码
        header.put("MAC_VALUE",fuxinbank01Header.MAC);

        json.put("SYS_HEAD",header);
    }

    protected String[] sendToBank(String p_reqStr, String transType, String commandCode) throws Exception {
        String[] retStr = new String[]{"", ""};
        if (StringHelper.hasAnyChar(transType) && StringHelper.hasAnyChar(commandCode)) {
            this.detailSimulateForBank(bankFront.bankCode, transType, commandCode, p_reqStr, retStr);
            if (retStr[1].length() > 0) {
                return retStr;
            }
        }
        WriteBankLogLn("1.提交银行的地址：" + bankFront.serverURL);
        WriteBankLogStep2(p_reqStr);
        WebRequest tmpWebRequest = new WebRequest(bankFront.serverURL);
        tmpWebRequest.setConnectTimeout(180000);//设置连接超时
        tmpWebRequest.setRequestProperty("Method", "POST");//设置请求属性
        tmpWebRequest.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//设置请求属性
        tmpWebRequest.setRequestProperty("connection", "close");//设置连接状态
        byte[] tmpUpBytes = p_reqStr.getBytes(getCharset());
        retStr = tmpWebRequest.upload(tmpUpBytes, getCharset());
        WriteBankLogStep3(retStr[1]);
        return retStr;

    }

    protected String getCharset() {
        return getBankConfig().getCharset("GB2312");
    }

    protected String getTimeStr(){
        return String.valueOf(System.currentTimeMillis());
    }

    @Override
    public void getContentLength(TcpReceiver p_receiver) throws Exception {
    }
}
